package funtastic.spellingbee;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;

public class SettingsActivity extends Activity
{

    private Spinner     mListNameSpinner;
    private RadioGroup  mListTypeRadioGroup;
    private SpellingBee mSpellingBee;

    public void cancelPreferences(View view)
    {
        finish();
    }

    public void displayListNames(View view)
    {
        String listType;

        if (mListTypeRadioGroup.getCheckedRadioButtonId() == R.id.radioButtonDefault)
        {
            listType = getString(R.string.list_type_default);
            mListTypeRadioGroup.check(R.id.radioButtonDefault);
        }
        else
        {
            listType = getString(R.string.list_type_custom);
            mListTypeRadioGroup.check(R.id.radioButtonCustom);
        }

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, mSpellingBee.getSpellingLists(listType));
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mListNameSpinner.setAdapter(adapter);

        int position = adapter.getPosition(mSpellingBee.getSpellingListName());
        mListNameSpinner.setSelection(position);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.settings_activity);

        mSpellingBee = (SpellingBee) getApplication();

        mListTypeRadioGroup = (RadioGroup) findViewById(R.id.RadioGroupDefaultOrCustom);
        mListNameSpinner = (Spinner) findViewById(R.id.SpinnerListNames);

        if (mSpellingBee.getSpellingListType().equals(getString(R.string.list_type_default)))
        {
            mListTypeRadioGroup.check(R.id.radioButtonDefault);
        }
        else
        {
            mListTypeRadioGroup.check(R.id.radioButtonCustom);
        }

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, mSpellingBee.getSpellingLists());
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mListNameSpinner.setAdapter(adapter);

        int position = adapter.getPosition(mSpellingBee.getSpellingListName());
        mListNameSpinner.setSelection(position);
    }

    public void savePreferences(View view)
    {
        String listType = (mListTypeRadioGroup.getCheckedRadioButtonId() == R.id.radioButtonDefault) ? getString(R.string.list_type_default) : getString(R.string.list_type_custom);
        String listName = "";

        if (mListNameSpinner.getSelectedView() != null)
        {
            TextView textView = (TextView) mListNameSpinner.getSelectedView();
            listName = textView.getText().toString();
        }

        mSpellingBee.setSpellingList(listType, listName);

        finish();
    }

}
